<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/11
 * Time: 9:53
 * 后台登陆
 */
class LoginController extends AdminController{
    public function __construct() {
    }
    public function beforeAction($action){
        return true;
    }

    public function actions(){
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xf4f4f4,
                'padding'=>0,
                'height'=>30,
                'minLength' => 4,
                'maxLength'=>4,
                'url' => '/webadmin/login/captcha'
            ),
        );
    }
    /**
     * 登陆页面
     */
    public function actionIndex(){
        $this->smarty->assign('captcha', $this->widget('CCaptcha',array('captchaAction'=> '/webadmin/login/captcha','showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer')), true));
        $this->smarty->display('login.tpl');
    }
    /**
     * 登陆
     */
    public function actionDo(){
        $username = $this->post('username');
        $pwd = $this->post('pwd');
        $code = $this->post('code');
        if (!$this->createAction('captcha')->validate($code, false)){
            $this->jump('验证码错误', 'webadmin/login');
        }
        $auth = new AdminAuthIdentity($username, $pwd);
        $result = $auth->authenticate();
        if ($result != $auth::LOGIN_SUCCESS) {
            $this->jump($this->Error($result), 'webadmin/login');
        }
        echo '<script>window.location.href="'.Yii::app()->createUrl('webadmin/index/index').'"</script>';exit;
    }
    /**
     * 退出登录
     */
    public function actionOut(){
        Yii::app()->session->clear();
        Yii::app()->session->destroy();
        header('Location:'.Yii::app()->createUrl('webadmin/login'));
    }
}
